<?php
function phorm_query($ORMObjectString)
{
	$config = PhormConfig::getConfig();
	$q      = NULL;
	
	switch($config->databaseType)
	{
		case 'mysql':
			$q = new MySQLQuery($ORMObjectString);
			break;
		case 'couchdb':
			$q = new MySQLQuery($ORMObjectString);
			break;
	}
	
		
	return $q;
}

function phorm_convert_array_to_object($ORMObjectString, &$array, &$target)
{
	$target        = new $ORMObjectString();
	$target->isNew = false;
	$alias         = strtolower($ORMObjectString);
	
	foreach($target->columns as $key=>$value)
	{
		if (!($value instanceof IIgnorableColumn))
		{
			if(array_key_exists($alias."_".$key, $array))
				$target->columns[$key]->value = $array[$alias."_".$key];
				
			if($value instanceof ForeignKey)
			{
				$newAlias = strtolower(get_class($value->relation));
				
				foreach($value->relation->columns as $altKey=>$altValue)
				{
					if (!($altValue instanceof IIgnorableColumn))
					{
						if(array_key_exists($newAlias."_".$altKey, $array))
							$target->columns[$key]->relation->$altKey = $array[$newAlias."_".$altKey];
					}
				}
			}
		}
	}
}
?>